lobsterh5.main({
    data: {
        loading: false, //状态标记
        page: 1,
        perPage: 5,
        flag: true,
        tabIndex: 0,
        patientlist: [],
        selectPatient: null,
    },
    pageload() {
        this.initdata();
        this.initevent();
    },
    initevent() {
        var self = this;
        // $('#t1').tab({
        //     defaultIndex: 0,
        //     activeClass: 'tab-green',
        //     onToggle: function (index) {
        //         $("#div_data").html("");
        //         console.log('index' + index);
        //         self.data.tabIndex = index;
        //         self.data.page = 1;
        //         self.data.flag = true;
        //         self.getrecord();
        //     }
        // })

        $(document).on("click", "#patiengdiv", function () {
            $("#half").popup();

            self.inittemp("tplcard", "sidebaritem", { list: self.data.patientlist });
        })

        $(document).on("click", ".carditem", function (obj) {
            var card = obj.currentTarget.dataset.info;
            var info = JSON.parse(card);
            self.data.selectPatient = info;
            //测试就诊人 （身份证：320***********9739）
            $("#selectedPatient").html(info.Name + "  (" + info.IDCard + " )");
            $.closePopup()
            $("#div_data").html("");
            self.data.page = 1;
            self.data.flag = true;
            self.getrecord();
        });

        //下拉加载数据
        $(document.body).infinite(100).on("infinite", function () {
            if (self.data.loading) return;
            self.data.loading = true;
            setTimeout(function () {
                self.data.page++;
                if (self.data.flag) {
                    self.getrecord();
                }
                self.data.loading = false;
            }, 1000);
        });

        $(document).on("click", ".itemdata", function (obj) {
            obj.stopPropagation();
            var info = $(this).data("info");
            console.log(info);
            location.href = "registerinfo.html?appid=" + lobsterh5.GetAppId() + "&id=" + info.Id;
        });

        $(document).on("click", "  .cancelregister", function (obj) {
            $.confirm("您确定要取消预约吗?", "确认取消?", function () {
                var info = JSON.parse(obj.currentTarget.dataset.info);
                self.cancelregister(info.Id);
            }, function () {
                //取消操作
            });
        });
    },
    initdata() {
        var self = this;

        self.getrecord();

        lobsterh5.GET("/admin/v1/CPatient/GetUserPatient").then(res => {
            self.data.patientlist = res.patdata;
        }).catch(res => {
            $.toast(res.msg, 'text');
        });
    },
    //取消预约
    cancelregister(id) {
        var self = this;
        lobsterh5.GETAPI("tsregister.cancelregister", { id: id }).then(res => {
            console.log(res);
            //  if (res && res.code ==0) {
            $("#div_data").html("");
            self.data.page = 1;
            self.getrecord();
            //}
        }).catch(res => {
            $.toast(res.msg, 'warning');
        })
    },
    getrecord() {
        var self = this;
        $('#more').html('<i class="weui-loading"></i><span class="weui-loadmore__tips">正在加载</span>');
        $('#more').show();
        var data = { "page": self.data.page, "limit": self.data.perPage };
        data.status = -1;
        if (self.data.tabIndex == 0) {
            data.status = 0;
        } else if (self.data.tabIndex == 1) {
            data.status = 1;
        }
        if (self.data.selectPatient) {
            data.PatientId = self.data.selectPatient.PatientId;
        }
        $.showLoading();
        lobsterh5.GETAPI("tsregister.getregrecordlist", data).then(res => {
            var data = res.list;
            if (data.length < self.data.perPage) {
                self.data.flag = false;//设置没有数据了标记
                //$(document.body).destroyInfinite();
                $("#more").html("没有更多数据了");
            }
            data.forEach(item => {
                if (item.RegState == 1) {
                    item.RegStateName = "预约中";
                } else if (item.RegState == 2) {
                    item.RegStateName = "预约成功";
                } else if (item.RegState == 3) {
                    item.RegStateName = "预约失败";
                } else if (item.RegState == 4) {
                    item.RegStateName = "预约取消";
                }
                item.CreateTime = item.CreateTime ? item.CreateTime.substring(0, 19) : "";
            })
            var html = template("tplrecord", { list: data });
            $("#div_data").append(html);

            $.hideLoading();
        })

        //this.inittemp("tplrecord", "div_data", { list: data });
    },
    inittemp(tempid, _id, data) {
        var html = template(tempid, data);
        $("#" + _id).html(html);
    },
}, true);